﻿using System;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Threading;
using System.Globalization;
using PMS.Common;

namespace BeiXun.Shared.UserControls
{
    public partial class LeftNavBar : System.Web.UI.UserControl
    {
        private const string Level1HtmlTemplate = @"
    <h3 onclick='leftnav_level1_onclick(this,event);'>
       <img src='{1}/submenu_o.gif' src_o='{1}/submenu_o.gif' src_c='{1}/submenu_c.gif' alt='' />{0}
    </h3>";

        private const string Level2HtmlTemplate = @"
    <li id='{3}'><a href='{0}' target='{4}' class='{2}'>{1}</a></li>";

        private const string normalCssClass = "";
        private const string selectedCssClass = "current";
        protected void Page_Load(object sender, EventArgs e)
        {

            if (this.leftSitemap.Provider.CurrentNode != null)
            {
                SiteMapNode tempNode = this.leftSitemap.Provider.CurrentNode;
                this.Page.Title = string.Empty;
                string strTemp = string.Empty;
                while (tempNode != null)
                {
                    if (tempNode.Title != strTemp)
                    {
                        strTemp = tempNode.Title;
                        this.Page.Title = strTemp + "--" + Page.Title;
                    }
                    tempNode = tempNode.ParentNode;
                }
                if (!string.IsNullOrEmpty(Page.Title))
                {
                    Page.Title = Page.Title.Substring(0, Page.Title.Length - 2);
                }
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            SiteMapNode current = this.leftSitemap.Provider.RootNode;
            SiteMapNode topNavNode = null;

            StringBuilder scripts = new StringBuilder();

            try
            {
                current = this.leftSitemap.Provider.CurrentNode.ParentNode.ParentNode;
                topNavNode = current;
            }
            catch { }

            if (topNavNode != null)
            {               
                int urlHashCode = topNavNode.Url.GetHashCode();
                scripts.AppendFormat(@"leftnav_set_topnav_selected('topnav_{0}');", urlHashCode).AppendLine();
            }

            scripts.AppendFormat(@"window.{1}=new BingoWeb_LeftNav('{0}');", this.ClientID, this.ID).AppendLine();

            if (current != null && current.HasChildNodes)
            {
                int i = 0, j = 0;
                string level1Id, level2Id;
                StringBuilder html = new StringBuilder();
                string strPur = "";

                if (Session["UserPurviews"] != null)
                    strPur = "" + Session["UserPurviews"].ToString();

                foreach (SiteMapNode node in current.ChildNodes)
                {
                    if (node.HasChildNodes)
                    {
                        bool hasPurview = false;
                        level1Id = "lnav_l1_" + (++i);
                        foreach (SiteMapNode childNode in node.ChildNodes)
                        {
                            //权限判断
                            if (strPur.Contains(childNode.Description) || childNode.Description == "200011"
                                || childNode.Description == "101010" || childNode.Description == "300011" 
                                || childNode.Description == "400011"|| childNode.Description == "101020"
                                || childNode.Description == "101030" || childNode.Description == "101040"
                                || childNode.Description == "101050" || childNode.Description == "101060") // 首页和个人信息不用权限控制
                            {
                                if (!hasPurview)
                                {
                                    hasPurview = true;
                                    html.AppendFormat(Level1HtmlTemplate, node.Title, "../../Images").AppendLine();
                                    html.AppendFormat("<ul id='{0}'>", level1Id);
                                }
                                string target = childNode["target"];
                                string url = childNode.Url;

                                if (target != null) target = target.Trim();
                                if (String.IsNullOrEmpty(target)) target = "_self";

                                level2Id = "lnav_l2_" + (++j);
                                bool b = childNode == this.leftSitemap.Provider.CurrentNode;
                                html.AppendFormat(Level2HtmlTemplate,
                                    Page.Server.HtmlEncode(url),
                                    Page.Server.HtmlEncode(childNode.Title),
                                    b ? selectedCssClass : normalCssClass,
                                    level2Id, target).AppendLine();

                                if (b)
                                {
                                    scripts.AppendFormat("{0}.setSelectedLevel1Item(document.getElementById('{1}'));", this.ID, level1Id).AppendLine();
                                    scripts.AppendFormat("{0}.setSelectedLevel2Item(document.getElementById('{1}'));", this.ID, level2Id).AppendLine();
                                }
                            }
                        }
                        html.Append("</ul>");
                    }
                }
                writer.Write(html.ToString());
            }

            writer.Write(string.Format("<script type='text/javascript'>{0}</script>", scripts));            
        }
    }
}